home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8315 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.0 KB  |  49 lines

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Memory allocation.
  5. Date: 16 Feb 1996 19:35:44 GMT
  6. Organization: Borland International
  7. Message-ID: <4g2mag$f81@druid.borland.com>
  8. References: <1996Feb10.161530.26449@wisipc.weizmann.ac.il>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <1996Feb10.161530.26449@wisipc.weizmann.ac.il>, cerlpvk says...
  15. >
  16. >I'm a starter so the question may seem stupid.
  17. >
  18. >Would someone explain why this program works?
  19.  
  20. Luck. Don't waste time understanding it. It is not legal code, because func() 
  21. returns a pointer to an auto object. The compiler I use generates a warning on 
  22. the return statement in func().
  23.  
  24. >
  25. >int* func()
  26. >{
  27. >  
  28. >  
  29. > int b[10];
  30. >   for(int i=0;i<9;i++)
  31. >     b[i]=i;
  32. >  return b;  
  33. >}
  34. >
  35. >int main(){
  36. >  
  37. > int* x= func();
  38. >  
  39. >  
  40. >  cout<<x[2]<<endl;
  41. >  return(1);
  42. >}
  43. >
  44. >The point is that b is allocated on the stack i.e without "new".
  45. >
  46. >Thanks in advance.
  47. >
  48.  
  49.